import 'package:enterprise_v6_app/app/components/custom_modele/custom_card.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_dropdown_selection.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/data/entities/application/major_hazard/hazard_obligation_mission_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/major_hazard_controller.dart';
import 'package:enterprise_v6_app/app/modules/application/major_hazard/obligation/obligation_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

class HazardObligationsPage extends StatelessWidget {
  HazardObligationsPage({super.key});

  final controller = Get.put(HazardObligationsController());

  MajorHazardController get hazardController => Get.find<MajorHazardController>();

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            body: Container(
              padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 14.w),
              alignment: Alignment.topCenter,
              color: const Color(0xffF5F5F6),
              child: Column(
                children: <Widget>[
                  GetBuilder(
                      init: hazardController,
                      builder: (_) {
                        return CustomDropdownSelection(
                          text: hazardController.selectedHazard?.riskObjectName,
                          hintText: '选择重大风险源',
                          onCallBack: () => hazardController.showHazardSelect(() {
                            controller.getMissions();
                            controller.getPersons();
                          }),
                        );
                      }),
                  AppGaps.vGap10,
                  _buildTop(),
                  AppGaps.vGap10,
                  Expanded(
                    child: controller.missions.isEmpty
                        ? const Center(
                            child: Text('暂无数据'),
                          )
                        : ListView.separated(
                            itemBuilder: (_, idx) => _buildExplain(controller.missions[idx], idx),
                            separatorBuilder: (_, __) => AppGaps.vGap10,
                            itemCount: controller.missions.length,
                          ),
                  ),
                ],
              ),
            ),
          );
        });
  }

  /// 最多显示四个
  Widget _buildTop() {
    if (controller.persons.isEmpty) return AppGaps.zero;
    final names = controller.persons.map((e) => e.nickName ?? '').toList();
    final urls = controller.persons.map((e) => e.avatar ?? '').toList();
    return CustomCard(
      padding: EdgeInsets.zero,
      titleWidget: SizedBox(
        height: 34.w,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [_buildAvatars(urls: urls), _buildName(name: names)],
        ),
      ),
      divider: CustomCard.builtinDivider,
      content: SizedBox(
        height: 28.w,
        child: _buildHints(),
      ),
    );
  }

  Widget _buildHints() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        _buildHint(AppImages.icon_select, '当前已完成'),
        _buildHint(AppImages.icon_delete, '当前未完成'),
      ],
    );
  }

  Widget _buildHint(String icon, String name) {
    return Row(
      children: [
        SvgPicture.asset(icon),
        AppGaps.hGap10,
        Text(
          name,
          style: TextStyle(
            color: AppColors.font_color_black,
            fontSize: 15.w,
            fontWeight: FontWeight.w500,
          ),
        )
      ],
    );
  }

  Widget _buildAvatars({List<String>? urls}) {
    return Row(
      children: urls?.map((e) => _buildAvatar(url: e)).toList() ?? [],
    );
  }

  /// 最多显示四个
  Widget _buildAvatar({String? url}) {
    return ClipOval(
      child: url != null && url.isNotEmpty
          ? Image.network(
              url,
              width: 26.w,
              height: 26.w,
              fit: BoxFit.cover,
            )
          : SvgPicture.asset(
              AppImages.icon_default_avatar,
              height: 26.w,
              width: 26.w,
            ),
    );
  }

  Widget _buildName({List<String>? name}) {
    final text = () {
      if (name == null || name.isEmpty) {
        return '无';
      }
      if (name.length <= 4) {
        return name.join('、');
      }

      return '${name.join('、')}...';
    }();
    return Text(
      text,
      style: TextStyle(fontSize: 12.sp, color: Colors.grey),
    );
  }

  Widget _buildExplain(HazardObligationMissionEntity item, int idx) {
    final finish = item.practicableStatus ?? false;
    return CustomCard(
      onTap: () => Get.toNamed(AppRoutes.majorHazardViolationPage, parameters: {
        'missionId': item.id!,
        'type': controller.type.id,
      }),
      radius: 13,
      padding: EdgeInsets.zero,
      divider: CustomCard.builtinDivider,
      titleWidget: _buildTitle(
        idx + 1,
        finish ? AppImages.icon_select : AppImages.icon_delete,
        finish ? '落实' : '待落实',
      ),
      content: Padding(
        padding: EdgeInsets.all(16.w),
        child: Text(
          item.riskMeasureDesc ?? '未知',
          style: TextStyle(color: Colors.grey, fontSize: 12.sp),
        ),
      ),
    );
  }

  Widget _buildTitle(int idx, String icon, String text) {
    return SizedBox(
        height: 48.w,
        child: Stack(
          alignment: Alignment.center,
          children: [
            Align(
              alignment: Alignment.topLeft,
              child: Stack(
                alignment: Alignment.center,
                children: [
                  SvgPicture.asset(
                    AppImages.icon_card_topleft_rect,
                    width: 20.w,
                    height: 20.w,
                  ),
                  Text(
                    idx.toString(),
                    style: TextStyle(
                      color: AppColors.font_color_blue,
                      fontSize: 12.w,
                    ),
                    textAlign: TextAlign.center,
                  ),
                ],
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceAround,
              children: [
                Row(
                  children: [
                    SvgPicture.asset(
                      icon,
                      width: 16.w,
                      height: 16.w,
                    ),
                    AppGaps.hGap10,
                    Text(
                      text,
                      style: TextStyle(
                        color: AppColors.font_color_black,
                        fontSize: 15.w,
                        fontWeight: FontWeight.w500,
                      ),
                    )
                  ],
                ),
                Stack(
                  alignment: Alignment.center,
                  children: [
                    SvgPicture.asset(AppImages.icon_card_topright_rect),
                    Text(
                      '历史逾期记录',
                      style: TextStyle(color: Colors.white, fontSize: 12.w),
                    ),
                  ],
                ),
              ],
            )
          ],
        ));
  }
}
